﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Windows.Forms;
using Data.Common;
using Data.Condition;
using Data.Enum;
using Data.Model;
using Data.ViewModel;
using DataAccess.BikeManage;
using DataAccess.Common;
using Ninject;
using WinForm.Helper;
using WinForm.Properties;

namespace WinForm.CarManage
{
    public partial class FrmParkingCheck : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly ILockArea _lockArea = Kernel.Get<ILockArea>();
        private readonly ILockPile _lockPile = Kernel.Get<ILockPile>();
        private readonly ILookUpDataService _lookUpDataService = Kernel.Get<ILookUpDataService>();

        public FrmParkingCheck()
        {
            InitializeComponent();
        }

        private void RefreshDatas()
        {
            string errorMessage = string.Empty;
            try
            {
                var condition = new SearchLockPileCondition
                {
                    LockPileNo = txtPileNo.Text,
                    LockAddressName = txtPakingName.Text,
                    LockAreaId = Converts.ConvertToInt(cmbArea.SelectedValue == null ? "" : cmbArea.SelectedValue.ToString()),
                    Status = Converts.ConvertToInt(cmbStatus.SelectedValue == null ? "" : cmbStatus.SelectedValue.ToString()),
                    ControlBoxNo = txtManageBoxNo.Text
                };
                ResultInfo<PagingDataSet<LockPileView>> lockpiles = _lockPile.GetLockPiles(condition,
                    xPagingBar1.PageIndex, xPagingBar1.PageSize);
                if (lockpiles.Obj.TotalRecords == 0)
                {
                    MyMessageBox.ShowMessageBoxInfo(Resources.NotFoundDatas);
                    return;
                }
                if (lockpiles.ResultNumber == ResultEnum.Success)
                {
                    SetDataPager(lockpiles);
                    dgvPaking.DataSource = lockpiles.Obj.Records;
                }
                else
                {
                    errorMessage = errorMessage + lockpiles.Describe;
                }
            }
            catch (SqlException)
            {
                errorMessage = Resources.StrErrorDB;
            }
            catch (Exception e)
            {
                errorMessage = errorMessage + e.Message;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     查询停车点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnParkingCheck_Click(object sender, EventArgs e)
        {
            RefreshDatas();
        }

        private void btnRest_Click(object sender, EventArgs e)
        {
            txtManageBoxNo.Clear();
            txtPakingName.Clear();
            txtPileNo.Clear();
            cmbArea.SelectedIndex = 0;
            cmbStatus.SelectedIndex = 0;
        }

        /// <summary>
        ///     初始化翻页控件信息
        /// </summary>
        /// <param name="lockpiles"></param>
        private void SetDataPager(ResultInfo<PagingDataSet<LockPileView>> lockpiles)
        {
            xPagingBar1.PageIndex = lockpiles.Obj.PageIndex;
            xPagingBar1.PageSize = lockpiles.Obj.PageSize;
            xPagingBar1.TotalRecordCount = lockpiles.Obj.TotalRecords;
            xPagingBar1.Bind();
        }

        private void FrmParkingCheck_Load(object sender, EventArgs e)
        {
            //分页控件初始化
            xPagingBar1.PageIndex = 1;
            xPagingBar1.PageSize = 20;
            xPagingBar1.xRefresh += RefreshDatas;
            IList<jsh_Area> areas = _lockArea.GetLockArea(0).Obj;
            areas.Insert(0, jsh_Area.All());
            cmbArea.DataSource = areas;
            cmbArea.DisplayMember = "Area";
            cmbArea.ValueMember = "id";


            List<IdNameObject> status = _lookUpDataService.GetLockaddressStatus();
            status.Insert(0, IdNameObject.All());
            cmbStatus.DataSource = status;
            cmbStatus.DisplayMember = "Name";
            cmbStatus.ValueMember = "Id";
        }
    }
}